package favorite

import (
	"context"

	"mall/common/globalkey"
	"mall/common/jwt"
	"mall/common/resultx"
	"mall/common/utils/pointy"
	"mall/service/forum/api/internal/svc"
	"mall/service/forum/api/internal/types"
	"mall/service/forum/rpc/forumclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddShopGoodsFavoriteLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddShopGoodsFavoriteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddShopGoodsFavoriteLogic {
	return &AddShopGoodsFavoriteLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddShopGoodsFavoriteLogic) AddShopGoodsFavorite(
	req *types.AddShopGoodsFavoriteReq,
	headers *[]*types.Header,
) (resp *types.BaseMsgResp, err error) {
	if req.ProductId == nil || *req.ProductId == 0 {
		return nil, resultx.NewErrCode(resultx.PRODUCT_ID_REQUIRED)
	}

	userId := jwt.GetUserId(l.ctx)
	if userId == 0 {
		return nil, resultx.NewErrCode(resultx.TOKEN_EXPIRE_ERROR)
	}

	_, err = l.svcCtx.ForumRpc.CreateUserCollect(l.ctx, &forumclient.UserCollectInfo{
		UserId:     pointy.GetPointer(userId),
		PlatformId: pointy.GetPointer(uint32(globalkey.ThirdPartyPlatformWechatMini)),
		ObjectId:   req.ProductId,
		ObjectType: pointy.GetPointer(uint32(globalkey.AttachmentLogTypeProduct)),
	})
	if err != nil {
		return nil, err
	}

	return &types.BaseMsgResp{
		Msg: "收藏成功",
	}, nil
}
